import axios from 'axios';

// const baseUrl = 'http://119.29.116.62:3000/api/v1';

const baseUrl = '/api/v1';

/**
 * 调用搜索的API
 *
 * @export
 * @param {string} keyword
 */
export function searchByKeyword(keyword) {
  return axios.get(`${baseUrl}/search`, {
    params: {
      keyword
    }
  });
}

/**
 * 调用搜索的lucene API
 *
 * @export
 * @param {string} query
 */
export function searchByDSL(query) {
  return axios.get(`${baseUrl}/searchDSL`, {
    params: {
      query
    }
  });
}

/**
 * 调用proxy
 *
 * @export
 * @param {any} data
 */
export function proxyQuery(data) {
  return axios.post(`${baseUrl}/proxyQuery`, data);
}

/**
 *
 * 通过id 获得菜谱
 * @export
 * @param {string} id
 */
export function getOneById(id) {
  return axios.get(`${baseUrl}`, {
    params: {
      id
    }
  });
}

/**
 * 获得coursels
 *
 * @export
 * @returns
 */
export function getCoursels() {
  return axios.get(`${baseUrl}/coursels`);
}

export function getLatest(from = 0) {
  return axios.get(`${baseUrl}/lastest`, {
    params: {
      from
    }
  });
}

export function getHotest(from = 0) {
  return axios.get(`${baseUrl}/hotest`, {
    params: {
      from
    }
  });
}

/**
 * 分类搜索
 *
 * @export
 * @param {string} type
 * @param {'date'|'fans'} sort ['date','fans']
 * @returns
 */
export function getCategoryByType(type, sort, from = 0) {
  return axios.get(`${baseUrl}/category`, {
    params: {
      type,
      sort,
      from
    }
  });
}

export function wordAnalyzer(text) {
  return axios.get(`${baseUrl}/wordAnalyze`, {
    params: {
      text
    }
  });
}

// curl -XGET "http://localhost:9200/cookbooks/_search" -H 'Content-Type: application/json' -d'
// {
//   "size": 0,
//   "aggs": {
//     "by_month": {
//       "date_histogram": {
//         "field": "date",
//         "interval": "month",
//         "format": "yyyy-MM"
//       },
//       "aggs": {
//         "fans_stats": {
//           "stats": {
//             "field": "fans"
//           }
//         }
//       }
//     }
//   }
// }'
